package com.framework.pages;

import com.framework.model.Movie;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

public class MoviePage extends AnyPage {

    public MoviePage(PageManager pages) { super(pages); }

    @FindBy (xpath = "//*[@id=\"content\"]/section/nav/ul/li[4]/div/div/a")
    private WebElement remove;

    public MoviePage ensurePageLoaded() {
        wait.until(presenceOfElementLocated(By.xpath("//*[@id=\"content\"]/section/nav/ul/li[4]/div/div/a")));
        return this;
    }

    public boolean movieTitleIsPresent(Movie movie) {
        return driver.getPageSource().contains(movie.getTitle());
    }

    public boolean isLoaded() {
        try {
            wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@id=\"content\"]/section/nav/ul/li[4]/div/div/a")));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public MainPage clickRemoveButton() {
        remove.click();
        wait.until(alertIsPresent()).accept();
        return pages.mainPage;
    }

}